#include "mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    setWindowTitle("鼠标事件测试");
    resize(1200, 1000);

    label1 = new QLabel;
    label1->setText("鼠标在当前窗口的坐标为: ");
    label1->setFixedWidth(200);

    label2 = new QLabel;
    label2->setText("123, 456");
    label2->setFixedWidth(200);

    //状态栏添加控件addPermanentWidget的顺序为 ： 从右向左开始
//    QLabel* label3 = new QLabel;
//    label3->setText("xxxxxxx");
//    label3->setFixedWidth(200);

//    statusBar()->addPermanentWidget(label3);

    //而状态栏的showMessage只能位于最左下角
    //statusBar()->showMessage("用户已按下鼠标[中键]坐标");
    //statusBar()->showMessage("xxxxxxxxxxxxx");

    statusBar()->addPermanentWidget(label1);
    statusBar()->addPermanentWidget(label2);


    setMouseTracking(true); // 设置鼠标实时追踪，否则mouseMoveEvent就失去效果了

    displayPicture();
}


MainWindow::~MainWindow()
{
}



void MainWindow::mouseMoveEvent(QMouseEvent* e)
{
    label2->setText("(" + QString().number(e->x()) + "," +
                    QString().number(e->y()) + ")");
}


void MainWindow::mouseReleaseEvent(QMouseEvent* e)
{
    QString str = "(" + QString().number(e->x()) + "," + QString().number(e->y()) + ")";
    statusBar()->showMessage("用户释放鼠标: " + str);
}


void MainWindow::mousePressEvent(QMouseEvent* e)
{
     QString str = "(" + QString().number(e->x()) + "," + QString().number(e->y()) + ")";

     if(e->button() == Qt::LeftButton)
     {
         statusBar()->showMessage("用户按下鼠标左键" + str);
     }
     else if(e->button() == Qt::RightButton)
     {
        statusBar()->showMessage("用户按下鼠标右键" + str);
     }
     else if(e->button() == Qt::MidButton)
     {
         statusBar()->showMessage("用户按下鼠标中间键" + str);
     }
}


void MainWindow::displayPicture()
{
   QString filename("C:/Users/2426/01.webp");

   QImage* image = new QImage;
   QLabel* imageLabel = new QLabel(this);

   imageLabel->move(40,40);
   imageLabel->setFixedSize(800,600);

   if(image->load(filename))
   {
       imageLabel->setPixmap(QPixmap::fromImage(*image));
   }
   else
   {
      QMessageBox::information(this, "失败", "加载图片失败");
      return;
   }
}





